<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="手册" value="1"></el-option>
              <!-- <el-option label="版本" value="2"></el-option> -->
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <!-- <el-table-column v-if="colData[0].istrue" min-width="40" align="center" prop="id" label="ID"></el-table-column> -->
      <el-table-column v-if="colData[0].istrue" min-width="130" align="center" prop="name" label="操作手册"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="130" align="center" prop="author" label="作者"></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="130" align="center" prop="download_count" label="下载次数"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="140" align="center" prop="create_time" label="上传时间">
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
      <el-table-column min-width="80" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getdownloadManual(scope.row)">下载</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination background :current-page.sync="listQuery.startSize" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryManual, downloadManual } from "@/api/handbook.js";
export default {
  components: { dynamicTable },
  data() {
    return {
      checkboxVal: ["操作手册", "作者", "下载次数", "上传时间"],
      key: 1,
      colData: [], //动态控制table
      //归属table
      browserTable: [],
      total: 0,
      searchTable: "",
      select: "1", //搜索条件
      browserTable: [],
      total: 0,
      listQuery: { startSize: 1, pageSize: 10, name: undefined, version: undefined },
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getqueryManual()
  },
  methods: {
    //操作手册下载
    getdownloadManual(data) {
    downloadManual({id: data.id}).then(res => {
        window.open(data.address)
    })
    },
    getqueryManual() {
      console.log('参数',this.listQuery)
      queryManual(this.listQuery).then(res => {
        this.browserTable = res.data
        this.total = res.total
      }).catch(err => {
        console.log('请求失败', err)
        this.browserTable = []
      })
    },
    //搜索,1:手册名，2:版本查询
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.name = this.searchTable
          this.getqueryManual();
          break;
        case '2':
          this.listQuery.version =  this.searchTable 
          this.getqueryManual();
          break;
      }
    },
    handleSizeChange(val) {
      this.getqueryManual();
    },
    handleCurrentChange(val) {
      this.getqueryManual();
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, name: undefined, version: undefined }
      this.getqueryManual()
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>